<?php

namespace App\Model\Common;

class Menu extends Common
{
    protected $perPage = 1000;
    protected $fillable = ['pid', 'title', 'url', 'icon', 'status', 'isshow', 'rank'];
    protected $cacheKey = '';
    protected $originCacheKey = '';
    protected $Group = null;

    public function getOriginMenus($opt = [])
    {
        $menus = cache($this->originCacheKey);

        if (empty($menus)) {
            $menus = $this->all();
            cache([$this->originCacheKey => $menus], 5);
        }

        return $menus;
    }

    public function getMenus($opt = [])
    {
        $menus = cache($this->cacheKey);

        if (empty($menus) || (!empty($opt['no_cache']) && true === $opt['no_cache'])) {
            $menus = $this->orderBy('rank', 'desc')->get();
            cache([$this->originCacheKey => $menus], 5);
            $data = [];
            foreach ($menus as $k => $v) {
                $data[$v['pid']][] = $v;
            }
            // return $data;
            if (empty($data)) {
                return [];
            }
            $menus = $this->menuFormat($data[0], $data);
            cache([$this->cacheKey => $menus], 5);
        }

        return $menus;
    }

    private static function menuFormat($da, $data)
    {
        foreach ($da as $k => $v) {
            if (isset($data[$v['id']])) {
                $da[$k]['children'] = array_values(self::menuFormat($data[$v['id']], $data));
            }
        }

        return $da;
    }

    public function getMyMenu($all = true)
    {
        $Group = new $this->Group();
        $pids = $Group->getPids();

        $menus = $this->getMenus();
        $menus = $this->_filterMenu($menus, $pids, $all);

        return array_values($menus);
    }

    protected static function _filterMenu($menus, $pids, $all)
    {
        foreach ($menus as $kk => $ms) {
            if (!$all && !$ms['isshow']) {
                unset($menus[$kk]);
                continue;
            }
            if (!in_array($ms['id'], $pids) && empty($ms['children'])) {
                unset($menus[$kk]);
                continue;
            } elseif (isset($ms['children']) && $ms['children']) {
                $menus[$kk]['children'] = self::_filterMenu($ms['children'], $pids, $all);
            }
            if (isset($menus[$kk]['children'])) {
                if (empty($menus[$kk]['children'])) {
                    unset($menus[$kk]['children']);
                    continue;
                } else {
                    $menus[$kk]['children'] = array_values($menus[$kk]['children']);
                }
            }
        }

        return $menus;
    }

    public function edit($request, $opt = [])
    {
        $re = $this->where($this->getKeyName(), $request->input('id'))->update($request->only($this->getColumns()));
        $this->getMenus(['no_cache' => true]);

        return $re;
    }

    public function refreshCache($menu, $mode = 'update')
    {
        $this->getMenus(['no_cache' => true]);
    }
}
